﻿using BarcodeDelay.bean;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BarcodeDelay.business
{
    public class ImageDelHelper
    {
        private StreamWriter swb = null;
        FileStream bFile = null;
        ArrayList m_arListDepBarcodes = new ArrayList();
        public event Action changeAddr;
        private string m_filePath = "";

        private static ImageDelHelper instance;
        public static ImageDelHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ImageDelHelper();
                }
                return instance;
            }
        }
        private Thread fThreadDep = null;
        public void BeginImgThread(string filePath)
        {
            try
            {
                m_filePath = filePath;
                initLog();
                m_arListDepBarcodes.Clear();
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    fThreadDep.Abort();
                    System.Threading.Thread.Sleep(50);
                }
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    while (fThreadDep.ThreadState != ThreadState.Aborted)
                    {
                        //当调用Abort方法后，如果thread线程的状态不为Aborted，主线程就一直在这里做循环，直到thread线程的状态变为Aborted为止
                        Thread.Sleep(100);
                    }
                }


                fThreadDep = new Thread(handleImg);
                fThreadDep.Start();

                fThreadDep.IsBackground = true;
              

            }
            catch (Exception ex)
            {

            }
        }



        public void handleImg()
        {
            try
            {
                while(true)
                {
                    Thread.Sleep(30000);
                    try
                    {
                        //Transaction.TransactionDatabase.Instance.deleteHistoryData();
                        if (!Directory.Exists(m_filePath))
                        {
                            continue;
                        }
                        DirectoryInfo directoryInfo = new DirectoryInfo(m_filePath);
                        ArrayList arListToDelete = new ArrayList();
                        int fileCount = 0;
                        foreach (FileSystemInfo fsi in directoryInfo.GetFileSystemInfos())
                        {
                            if (fsi is FileInfo)
                            {
                                string fileNmae = fsi.Name;//获取文件名
                          
                                DateTime dtCreate = fsi.CreationTime;
                                DateTime dtNow = System.DateTime.Now;
                                TimeSpan ts = dtNow.Subtract(dtCreate);
                                double minutes = ts.TotalMinutes;
                                if (minutes > 5)
                                {
                                    fileCount++;
                                    if (fileCount <= 3000)
                                    {
                                        arListToDelete.Add(fsi.FullName);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
      
                       

                        foreach (string fullFileName in arListToDelete)
                        {
                            File.Delete(fullFileName);
                        }
                    }
                    catch (Exception ex)
                    {
                        
                    }
                    
                }

            }
            catch(Exception ex)
            {

            }
        }







        public void stopThread()
        {
            try
            {
                
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                  
                    fThreadDep.Abort();
                    if (swb != null)
                    {
                        swb.Close();
                    }

                }

    
            }
            catch (Exception ex)
            {

            }
        }


        private void initLog()
        {
            try
            {
                string dir = System.Environment.CurrentDirectory;
                bFile = new FileStream(dir + "\\error.txt", FileMode.Create);
                if (swb == null)
                {
                    swb = new StreamWriter(bFile);
                    swb.WriteLine("begin");

                }
            }
            catch (Exception ex)
            {

            }

        }


    }
}
